package com.controller.student;

import java.io.File;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Cstudent;
import com.po.ImageObject;
import com.po.SessionC;
import com.service.student.StudentServiceC;


@Controller
@Scope(value="prototype")
public class StudentControllerC {
	
	@Resource(name="studentServiceImpC")
	private StudentServiceC studentservice;

	@ResponseBody
	@RequestMapping(value="/findStudent")
	public Cstudent findStudent(@RequestBody Cstudent stu){
		
		return studentservice.findAllStudent(stu);
	}

	/*用户登录*/
	@ResponseBody
	@RequestMapping(value="/loginUser")
	public Object loginStudent(@RequestBody Cstudent stu,HttpServletRequest request){
		SessionC session = new SessionC();
		String sessionId = request.getSession().getId();
		
		int login = studentservice.checkLogin(stu);
		if(login==1){
			Cstudent stuallinfo = studentservice.findAllStudent(stu);
			request.getSession().setAttribute("userId", stu.getStuid());
			request.getSession().setAttribute("userPassword", stu.getStupwd());
			session.setLoginId(login);
			session.setSessionId(sessionId);
			session.setStuid(stuallinfo.getStuid());
			session.setStuname(stuallinfo.getStuname());
			session.setStusid(stuallinfo.getStusid());
			session.setStupwd(stuallinfo.getStupwd());
			session.setStuimpath(stuallinfo.getStuimpath());
			session.setStusex(stuallinfo.getStusex());
			session.setStuclass(stuallinfo.getStuclass());
		}else if(login==2){
			session.setLoginId(login);
			session.setSessionId(sessionId);
		}
		
		return session;
	}
	
	/*用户注册*/
	@ResponseBody
	@RequestMapping(value="/registerStudent")
	public int registerStudent(@RequestBody Cstudent stu){
		
		int register = studentservice.registerStudent(stu);
		return register;
	}
	
	/*验证注册用户名是否可用*/
	@ResponseBody
	@RequestMapping("/validataStudent")
	public int selectStudent(@RequestBody String stuid,HttpServletResponse response){
		
		Cstudent cstu = new Cstudent();
		cstu.setStuid(stuid);
		int validataStudent = studentservice.getReStudent(cstu);
		if(validataStudent == 1)
		{
			return 0;
		}
		return 1;
	}
	
	/*用户修改*/
	@ResponseBody
	@RequestMapping(value="/updateStudent")
	public int updateStudent(@RequestBody Cstudent stu){
		
		int update = studentservice.updateStudent(stu);
		return update;
	}
	/*清除session*/
	@ResponseBody
	@RequestMapping(value="/clearSession")
	public void logOut(HttpServletRequest request,HttpServletResponse response)
	{
		request.getSession().removeAttribute("userId");
		request.getSession().removeAttribute("userPassword");
	}
	
	@ResponseBody
	@RequestMapping(value = "/simages")
	public int simages(@RequestBody ImageObject image,String state,String id,HttpServletRequest request) {
		byte[] imagebyte=image.getImage();
		if(state.equals("0")){
			Cstudent cstu = new Cstudent();
			cstu.setStuid(id);
			cstu.setStuimpath(image.getImagename());
			studentservice.updateStudent(cstu);
		}
		System.out.println("**************"+request.getServletContext().getRealPath("/student/userinfo/")+image.getImagename());
		int i=byte2image(imagebyte,request.getServletContext().getRealPath("/student/userinfo/")+image.getImagename());
		return i;
	}
	public  int byte2image(byte[] data, String path) {
		if (data.length < 3 || path.equals(""))
			return 0;
		try {
			FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
			imageOutput.write(data, 0, data.length);
			imageOutput.close();
			System.out.println("Make Picture success,Please find image in " + path);
			return 1;
			
		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
			return 0;
		}
	}
	
}
